﻿
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Admin.EcsWeb.Entity;
using Mapster;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using Admin.EcsWeb.Service.PatProduct.Dto;
using Microsoft.AspNetCore.Http;

namespace Admin.EcsWeb.Service;
/// <summary>
/// PatProductService服务
/// </summary>
[ApiDescriptionSettings("ECS应用", Order = 110)]
public class PatProductService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Entity.PatProduct> _rep;
    public PatProductService(SqlSugarRepository<Entity.PatProduct> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询PatProductService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<PatProductOutput>> Page([FromQuery] PatProductInput input)
    {
        var query = _rep.AsQueryable()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.NAME), u => u.NAME.Contains(input.NAME.Trim()))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.DES), u => u.DES.Contains(input.DES.Trim()))

                    .Select<PatProductOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加PatProductService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddPatProductInput input)
    {
        var entity = input.Adapt<Entity.PatProduct>();
        await _rep.InsertAsync(entity);
    }


    /// <summary>
    /// 删除PatProductService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeletePatProductInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.ID) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.DeleteAsync(entity);   //删除
    }

    /// <summary>
    /// 更新PatProductService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdatePatProductInput input)
    {
        var entity = input.Adapt<Entity.PatProduct>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取PatProductService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<Entity.PatProduct> Get([FromQuery] QueryByIdPatProductInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.ID);
    }

    /// <summary>
    /// 获取PatProductService列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<PatProductOutput>> List()
    {
        return await _rep.AsQueryable().Select<PatProductOutput>().ToListAsync();
    }
    /// <summary>
    /// 获取产品
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Product")]
    [AllowAnonymous]
    public async Task<List<PatProductSimple>> GetProducts()
    {
        var datas= await _rep.AsQueryable().Select<PatProductSimple>().ToListAsync();
        return datas;
    }

    






}

